
Imports System.Diagnostics
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.VisualBasic
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class frmListStatTest

#Region " Variable "
    Dim drRev As New ERP.DRREVIEW
    Dim cn As New ERP.CONNECTION
    Private frmSplash As frmLoading
    Dim tbCustomer, tbPartName, tbListStatTest As New DataTable
    Public Delegate Sub UpdateUIDelegate(ByVal IsDataLoaded As Boolean)
#End Region

    Sub getDataToGrid()
        tbListStatTest = drRev.fillDataset(cn.OpenConnectionSQL("admin"), "spFGM", "cboCustomer", New String() {"@flag"}, New Object() {6}).Tables(0)
    End Sub

    Sub getDataToComboBox()
        tbCustomer = drRev.fillDataset(cn.OpenConnectionSQL("admin"), "spFGM", "cboCustomer", New String() {"@flag"}, New Object() {3}).Tables(0)
        tbPartName = drRev.fillDataset(cn.OpenConnectionSQL("admin"), "spFGM", "cboPartName", New String() {"@flag"}, New Object() {2}).Tables(0)
    End Sub

#Region " Delegate "
    Private Sub DoSomeWork()
        getDataToGrid()
        getDataToComboBox()
        Invoke(New UpdateUIDelegate(AddressOf UpdateUI), New Object() {True})
    End Sub

    Private Sub LoadData()
        getDataToGrid()
        Invoke(New UpdateUIDelegate(AddressOf UpdateUI), New Object() {True})
    End Sub

    Private Sub UpdateUI(ByVal IsDataLoaded As Boolean)
        If (IsDataLoaded) Then
            If (Not IsNothing(Me.frmSplash)) Then
                frmSplash.Close()
            End If

        End If
    End Sub
#End Region
    Private Sub frmListStatTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        frmSplash = New frmLoading()
        Dim t As Threading.Thread = New Threading.Thread(New Threading.ThreadStart(AddressOf DoSomeWork))
        t.IsBackground = True
        t.Start()
        frmSplash.ShowDialog()
        t.Abort()
        Me.dgrViewAll.DataSource = tbListStatTest
        cboCustomer.Properties.DataSource = tbCustomer
        cboCustomer.Properties.DisplayMember = "custno"
        cboCustomer.Properties.ValueMember = "custno"

        cboPartName.Properties.DataSource = tbPartName
        cboPartName.Properties.DisplayMember = "partName"
        cboPartName.Properties.ValueMember = "partName"
    End Sub

#Region " Button addnew "
    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        If Me.cboCustomer.Text.Trim = "" Then
            MsgBox("Please select Customer!")
            Me.cboCustomer.Focus()
            Exit Sub
        ElseIf Me.cboPartName.Text.Trim = "" Then
            MsgBox("Please select Part#!")
            Me.cboCustomer.Focus()
            Exit Sub
        Else
            drRev.SP_Capnhat(cn.OpenConnectionSQL("admin"), "spFGM", New String() {"@flag", "@custid", "@partname", "@comment"}, _
            New Object() {4, Me.cboCustomer.Text.Trim, Me.cboPartName.Text.Trim, Me.txtComment.Text.Trim})
            frmSplash = New frmLoading("")
            Dim t As Threading.Thread = New Threading.Thread(New Threading.ThreadStart(AddressOf LoadData))
            t.IsBackground = True
            t.Start()
            frmSplash.ShowDialog()
            t.Abort()
            Me.dgrViewAll.DataSource = tbListStatTest
        End If
    End Sub
#End Region

#Region " Button delete "
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If MessageBox.Show("Do you want to delete records: Customer=" & Me.danhsach.GetRowCellValue(Me.danhsach.FocusedRowHandle, "customer") & " and Partname=" _
        & Me.danhsach.GetRowCellValue(Me.danhsach.FocusedRowHandle, "partname"), "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
            drRev.SP_Capnhat(cn.OpenConnectionSQL("admin"), "spFGM", New String() {"@flag", "@custid", "@partname"}, _
                   New Object() {5, Me.danhsach.GetRowCellValue(Me.danhsach.FocusedRowHandle, "customer"), Me.danhsach.GetRowCellValue(Me.danhsach.FocusedRowHandle, "partname")}) ' Me.cboCustomer.Text.Trim, Me.cboPartName.Text.Trim})
            frmSplash = New frmLoading("")
            Dim t As Threading.Thread = New Threading.Thread(New Threading.ThreadStart(AddressOf LoadData))
            t.IsBackground = True
            t.Start()
            frmSplash.ShowDialog()
            t.Abort()
            Me.dgrViewAll.DataSource = tbListStatTest
        End If

    End Sub
#End Region


End Class